# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
submit_search_form = (page_no) ->
    form = $('form#search_form')
    if page_no
        form.form 'set value', 'page_no', page_no
    page_rows = Cookies.get 'table.page_rows'
    if not page_rows
        page_rows = 10
        Cookies.set 'table.page_rows', page_rows, expires: 365, path: "/"
    form.form 'set value', 'page_rows', page_rows
    form.submit()

$ ->
    # 当查询关键字改变之后，重置到第一页开始查询
    $('.ui.search input[name=keyword]').change ->
        $('form.ui.form#search_form').form 'set value', 'page_no', 1

    # 查询输入回车拦截
    $('.ui.search input[name=keyword]').keypress (evt) ->
        if evt.which == 13
            evt.preventDefault()
            submit_search_form 1

    # 查询按钮事件
    $('.ui.search .icon.search').click ->
        $(this).addClass 'loading'
        submit_search_form 1

    # 查询条件改变时触发提交
    $('form#search_form .dropdown').dropdown
        onChange : (value, text, $choice) ->
            submit_search_form 1

    # 清除查询日期
    $('form#search_form .ui.calendar .icon#date_clear').click ->
        $('form#search_form').form 'set values',
            start_date: '', end_date: ''

    ###
    # 设置表格底部的翻页控件跨越整行
    n_cols = $("table#table > thead").find("> tr:first > th").length
    $('tfoot > tr > th').attr('colspan', n_cols)
    ###

    # 重置每页行数
    page_rows = Cookies.get 'table.page_rows'
    if page_rows
        $('.ui.pagination .dropdown#page_rows').dropdown 'set value', page_rows

    # 每页行数下拉框改变
    $('.ui.pagination .dropdown#page_rows').dropdown
        onChange : (value, text, $selected) ->
            $(this).addClass 'loading'
            Cookies.set 'table.page_rows', value, expires: 365, path: "/"
            submit_search_form 1

    # 分页按钮事件
    $('.ui.pagination a.item').click ->
        page_no = $(this).data('page-no')
        submit_search_form page_no

    # 分页输入页码回车事件
    $('.ui.pagination input').keypress (evt) ->
        if evt.which == 13
            page_no = parseInt $(this).val()
            if isNaN page_no
                alert '您输入的页码 "' + $(this).val() + '" 格式有误，' +
                      '页码必须是介于 1 和 最大页码数之间的正整数。'
                return
            return if page_no < 1 or page_no > $('#page_num').data('page-num')
            return if page_no == $('form.form#search_form input[name="page_no"]').val()
            submit_search_form page_no

    # 当点击行时设置行的状态为活动，从视觉上以示区分
    $('table.table#table tbody tr').click ->
        $(this).toggleClass 'active'
        if $(this).hasClass 'active'
            if Cookies.get('settings.table_active_unique') is 'on'
                $(this).siblings('tr').removeClass 'active'

    # 展开／折叠详细信息
    $('i.detail.expand').click ->
        detail = $(this).parents('tr').next('tr.detail')
        if detail.is(':visible')
            detail.hide('fast')
            $(this).removeClass('down').addClass('right')
        else
            $('i.down').removeClass('down').addClass('right')
            $('table tr.detail').hide()
            detail.show('fast')
            $(this).removeClass('right').addClass('down')
